package com.CampusLife.controller;

import  com.CampusLife.bean.Product;
import com.CampusLife.bean.ProductType;
import com.CampusLife.service.IProductTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.IProductService;
import  com.CampusLife.until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
@Resource
    private IProductService iProductService;
@Resource
 private IProductTypeService iProductTypeService;
@RequestMapping("/queryAll")
public  String  findall(Model  model){
    List<Product> products=iProductService.getProductAll();
    model.addAttribute("products",products);
    List<ProductType> list=iProductTypeService.queryAll();
    model.addAttribute("list",list);
    return  "admin/shopbasic";
}
    @RequestMapping("/findAllShen")
    public  String  findallshen(Model  model){
        List<Product> products=iProductService.getProductAllShen();
        model.addAttribute("products",products);
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "admin/shenhe";
    }
@RequestMapping("getProductByType")
public String getProductByType(Integer t_id,Model model){
    List <Product> products=iProductService.getProductByType(t_id);
    model.addAttribute("products",products);
    List<ProductType> list=iProductTypeService.queryAll();
    model.addAttribute("list",list);
    return  "admin/shopbasic";
}
public String priduct(){
    return "admin/addshop";
}
@RequestMapping(path = "/addShop", method = RequestMethod.POST)
public String addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response,HttpSession httpSession, ModelMap model,Float price,String phone,String describe_txt,Integer t_id,String p_name) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
      String photo=  imagesUpload.Upload(file);
            Product product=new Product();
            product.setT_id(t_id);
            product.setPrice(price);
            product.setDescribe_txt(describe_txt);
            product.setPhone(phone);
            product.setP_name(p_name);
            product.setStatus(0);
            product.setPhoto(photo);
            DataTime dataTime=new DataTime();
            product.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            product.setId(i);
            iProductService.add(product);
        //设置消息 并跳转页面
        return "/admin/index";
    }
    //跳转到修改页面
    @RequestMapping("/jumpUpdateUserEdit")
    public String jumpUpdateUserEdit(Integer pid,Model model){

        //数据回显
     Product product=iProductService.queryById(pid);
       model.addAttribute("product",product);
        return "jsp/update";
    }
    //修改·用户·    /图片上传
    @RequestMapping(path = "/updateProduct", method = RequestMethod.POST)

    public String updateProduct(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response,HttpSession httpSession, ModelMap model,Float price,String phone,String describe_txt,Integer t_id,String p_name) throws Exception {
              ImagesUpload imagesUpload =new ImagesUpload();
              String photo= imagesUpload.Upload(file);
            Product product=new Product();
            product.setT_id(t_id);
            product.setPrice(price);
            product.setDescribe_txt(describe_txt);
            product.setPhone(phone);
            product.setP_name(p_name);
            product.setStatus(0);
            product.setPhoto(photo);
            DataTime dataTime=new DataTime();
            product.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            product.setId(i);
            iProductService.add(product);

        return "jsp/cc";
    }
    @RequestMapping("/deleteUser")
    public String deleteUser( Integer pid){

      iProductService.delete(pid);
        return "redirect:/user/query";
    }
}
